package com.zzvcom.service.admin;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zzvcom.persist.model.admin.ProductQuality;
import com.zzvcom.template.hibernate.DefaultManager;

@Service
@Transactional
public class ProductQualityManager extends DefaultManager<ProductQuality, String>
{

    @Transactional
    public void syncLine(String quality)
    {
        DetachedCriteria dc = DetachedCriteria.forClass(ProductQuality.class);     
        dc.add(Restrictions.eq("quality", quality));
        if (this.countByCriteria(dc) == 0)
        {
            ProductQuality pq= new ProductQuality();
            pq.setQuality(quality);
            this.save(pq);
        }
    }
    @Override
    public void save(ProductQuality entity)
    {
        
        if(StringUtils.isNotBlank(entity.getId()))
        {
            // update literal value of quality property
            ProductQuality old=this.get(entity.getId());
            this.updateByHQL(" update Product set quality=? where quality=?", entity.getQuality(),old.getQuality());    
            this.entityDao.evict(old);
        }
        super.save(entity);
    }
}
